package com.biz.primus.ms.promotion.dao.po;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.base.enums.PromotionTypeEnum;
import com.biz.primus.model.promotion.exception.PromotionExceptionType;
import com.biz.primus.model.promotion.vo.CutPriceStairVo;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.model.promotion.vo.ProductCutPriceVo;
import com.biz.primus.model.promotion.vo.req.OrderPromotionReqDetailVo;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 降价拍促销
 *
 * @author david-liu
 */
@Entity
@Getter
@Setter
public class CutPriceSalePromotion extends BasePromotion {
    private static final long serialVersionUID = 2480714600307808166L;

    /**
     * 降价段数
     */
    private Integer cutPriceStair;

    /**
     * 降价拍时间段详情信息, 存放降价拍时间段Vo集合的Json信息
     *
     * @see List <CutPriceStairVo>
     */
    private String cutPriceRule;

    @Override
    public boolean isMatchPromotionRule(OrderPromotionReqDetailVo detailVo) {
        AssertUtils.isTrue(Objects.nonNull(detailVo), PromotionExceptionType.ILLEGAL_PARAMETER);
        detailVo.postProcess();
        List<CutPriceStairVo> cutPriceStairVos = this.getCutPricePromotionRule();
        if (CollectionUtils.isEmpty(cutPriceStairVos)) {
            return false;
        }
        Optional<CutPriceStairVo> cutPriceStairVo = cutPriceStairVos.stream().filter(CutPriceStairVo::isNowInPromotionTimeRange).findFirst();
        return cutPriceStairVo.isPresent();
    }

    @Override
    public OrderPromotionDetailVo getOrderPromotionDetail(OrderPromotionReqDetailVo reqDetailVo) {
        AssertUtils.isTrue(Objects.nonNull(reqDetailVo), PromotionExceptionType.ILLEGAL_PARAMETER);
        reqDetailVo.postProcess();
        if (!isMatchPromotionRule(reqDetailVo)) {
            return null;
        }
        List<Long> promotionEffectProductIds = this.getPromotionEffectProductIds(reqDetailVo);
        if (CollectionUtils.isEmpty(promotionEffectProductIds)) {
            return null;
        }
        Optional<CutPriceStairVo> cutPriceStairVo = this.getCutPricePromotionRule().stream().filter(CutPriceStairVo::isNowInPromotionTimeRange).findFirst();
        AssertUtils.isTrue(cutPriceStairVo.isPresent(), PromotionExceptionType.ILLEGAL_STATE);
        OrderPromotionDetailVo orderPromotionDetailVo = new OrderPromotionDetailVo();
        orderPromotionDetailVo.setId(this.getId());
        orderPromotionDetailVo.setName(this.getName());
        orderPromotionDetailVo.setPromotionType(PromotionTypeEnum.CUT_PRICE_SALE);
        List<ProductCutPriceVo> cutPriceVos = promotionEffectProductIds.stream().map(productId -> new ProductCutPriceVo(productId, cutPriceStairVo.get().getSalePrice())).collect(Collectors.toList());
        orderPromotionDetailVo.setProductCutPrices(cutPriceVos);
        return orderPromotionDetailVo;
    }

    @SuppressWarnings("unchecked")
    public List<CutPriceStairVo> getCutPricePromotionRule() {
        if (StringUtils.isBlank(this.cutPriceRule)) {
            return Collections.emptyList();
        }
        return JsonUtils.json2Obj(this.cutPriceRule, List.class);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }
}
